package com.wtwd.campus.h5_manager.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.h5_manager.dao.H5ManageRoleMapper;
import com.wtwd.campus.h5_manager.entity.H5ManageRole;
import com.wtwd.campus.h5_manager.entity.vo.H5RolePermissionVo;
import com.wtwd.campus.h5_manager.service.H5ManageRoleService;
import com.wtwd.campus.h5_manager.service.H5ManagerUserService;
import com.wtwd.campus.h5_manager.service.H5MangerSysRolePermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2022-03-04
 */
@Service
public class H5ManageRoleServiceImpl implements H5ManageRoleService {


    @Autowired
    private H5ManageRoleMapper roleMapper;

    @Autowired
    private H5MangerSysRolePermissionService rolePermission;

    @Autowired
    private H5ManagerUserService userService;

    @Override
    public Integer addRole(H5ManageRole role) {
        return roleMapper.insert(role);
    }

    @Override
    public H5ManageRole getRoleByRoleId(int roleId) {
        return new LambdaQueryChainWrapper<>(roleMapper)
                .eq(H5ManageRole::getRoleId, roleId)
                .one();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteByCustomerId(Integer customerId) {
        //查询该客户下所有角色
        List<H5ManageRole> h5ManageRoles = this.queryRoleByCustomerId(customerId);
        List<Integer> roleIdList = h5ManageRoles.stream().map(H5ManageRole::getRoleId).collect(Collectors.toList());
        if (roleIdList == null || roleIdList.size() == 0) {
            return 0;
        }
        return this.deleteByRoleIdList(roleIdList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteByRoleId(Integer roleId) {
        rolePermission.deleteByRoleId(roleId);
        userService.deleteByRoleId(roleId);
        return roleMapper.deleteById(roleId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteByRoleIdList(List<Integer> roleIdList) {
        rolePermission.deleteByRoleIdList(roleIdList);
        return roleMapper.deleteByIdList(roleIdList);
    }

    @Override
    public List<H5ManageRole> queryRoleByCustomerId(Integer customerId) {
        return new LambdaQueryChainWrapper<>(roleMapper)
                .eq(H5ManageRole::geth5Customer, customerId)
                .list();

    }

    @Override
    public IPage<H5ManageRole> queryRoleByCustomerIdPage(Integer customerId, Page<H5ManageRole> page) {
        return roleMapper.queryRoleByCustomerIdPage(customerId, page);
    }

    @Override
    public List<H5RolePermissionVo> getRolePermission(Integer userRoleId, Long authRoleId) {
        List<H5RolePermissionVo> h5RolePermissionVos = roleMapper.getRolePermission(userRoleId, authRoleId);
        if (h5RolePermissionVos != null && h5RolePermissionVos.size() > 0) {
            for (H5RolePermissionVo rolePermissionVo : h5RolePermissionVos) {
                if (StringUtils.isBlank(rolePermissionVo.getCheckRole())) {
                    rolePermissionVo.setHasPermission(false);
                } else {
                    rolePermissionVo.setHasPermission(true);
                }
            }
        }
        return h5RolePermissionVos;
    }

    @Override
    public Integer addRolePermission(Integer roleId, List<Integer> perIds) {
         Integer rest = 0;
         rest =rolePermission.deleteByRoleId(roleId);
        if (perIds != null && perIds.size() > 0) {
            rest += rolePermission.bathAddRolePermission(roleId, perIds);
        }
        return rest;
    }

    @Override
    public List<H5ManageRole> queryRoleByCustomerIds(List<Integer> customers) {
        return new LambdaQueryChainWrapper<>(roleMapper)
                .in(H5ManageRole::geth5Customer, customers)
                .list();
    }

}
